#ifndef _GATEWAY_HPP_20120622
#define _GATEWAY_HPP_20120622

#include <ext/hash_map>

#include "singleton.hpp"
#include "acceptor_manager.hpp"
#include "connection.hpp"
#include "timer.hpp"

#include "confsvr_cmd.hpp"
#include "basesvr.hpp"
#include "scence_processor.hpp"

using namespace mina;
using namespace cmd;

class Scence;
class Player;

typedef SSGatewayRes GatewayConf;

class Gateway : public BaseSvr, public Singleton<Gateway>
{
	public :
		friend class Singleton<Gateway>;

	public :
		typedef AcceptorManager  Reactor;

		typedef __gnu_cxx::hash_map<RID, Player*> PlayerMap;
		typedef PlayerMap::iterator PlayerMap_Iter;

	public :
		virtual ~Gateway() { finish(); }

		template <typename _Msg>
		int handle(Connection & conn, _Msg & msg)
		{
			assert(false);
		}

		// should implement those function
		virtual int impl_init();
		virtual int process();
		virtual int reload();
		virtual int finish();
	
		// @TODO 
		void del_scence(Connection & conn) {}
		void add_scence(Connection & conn) {}

		Reactor * reactor()               { return _reactor; }

		int send_to_scence(Scence * scence, void * data);
		int send_to_player(Player * player, void * data);
		int send_data(Connection & conn, void * data);

		uint32_t sequence() { return (++ _sequence); }

	public :
		Reactor         *      _reactor;

		GatewayConf            _conf;

		static ScenceProcessor        _scence_processor;
		static Processor<Gateway>     _client_processor;
		static Processor<Gateway>     _http_processor;
		static Processor<Gateway>     _dbsvr_processor;
		static Processor<Gateway>     _confsvr_processor;

		CmdParser              _parser;

		uint32_t           _sequence;
		
		PlayerMap          _player_map;
};

#endif

